package com.lcu.wl.modules.mnt.controller;


import com.lcu.wl.common.utils.MapUtils;
import com.lcu.wl.common.utils.PageUtils;
import com.lcu.wl.common.utils.SYSDATA;
import com.lcu.wl.modules.mnt.entity.Log;
import com.lcu.wl.modules.mnt.service.ILogService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author wanglei
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/mnt/log")
public class LogController {

    @Autowired
    private ILogService iLogService;

    /**
     * 日志查询
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("日志查询")
    @RequiresPermissions("mnt:log:list")
    public SYSDATA list(@RequestParam Map<String,Object> params){
        PageUtils page = iLogService.queryPage(params,"INFO");
        return SYSDATA.ok().put("page",page);
    }

    /**
     * 导出数据
     * @param params
     * @return
     */
    @com.lcu.wl.annotation.Log("导出日志数据")
    @ApiOperation("日志导出")
    @GetMapping("/download")
    @RequiresPermissions("mnt:log:export")
    public void download(@RequestParam Map<String,Object> params, HttpServletResponse response) throws IOException {
        List<Log> logs = iLogService.queryAll(params,"INFO");
        iLogService.download(logs,response);
    }

    /**
     * 删除所有INFO日志
     * @return
     */
    @com.lcu.wl.annotation.Log("删除所有INFO日志")
    @ApiOperation("删除所有日志")
    @DeleteMapping("/delete")
    @RequiresPermissions("mnt:log:delete")
    public SYSDATA deleteInfo(){
        iLogService.removeByMap(new MapUtils().put("log_type","INFO"));
        return SYSDATA.ok();
    }

    /**
     * 异常日志查询
     * @param params
     * @return
     */
    @GetMapping("/error/list")
    @ApiOperation("异常日志查询")
    @RequiresPermissions("mnt:errLog:list")
    public SYSDATA errorList(@RequestParam Map<String,Object> params){
        PageUtils page = iLogService.queryPage(params,"ERROR");
        return SYSDATA.ok().put("page",page);
    }

    /**
     * 获取异常日志详情
     * @param logId
     * @return
     */
    @GetMapping("/error/{logId}")
    @ApiOperation("根据logId获取异常日志详情")
    public SYSDATA getErrorLog(@PathVariable Long logId){
        Object errDetail = iLogService.getByErrDetail(logId);
        return SYSDATA.ok().put("errDetail",errDetail);
    }

    /**
     * 导出异常日志
     * @param params
     * @param response
     * @throws IOException
     */
    @com.lcu.wl.annotation.Log("导出异常日志数据")
    @ApiOperation("异常日志导出")
    @GetMapping("/error/download")
    @RequiresPermissions("mnt:errLog:export")
    public void downloadErrLog(@RequestParam Map<String,Object> params, HttpServletResponse response) throws IOException {
        List<Log> logs = iLogService.queryAll(params,"ERROR");
        iLogService.download(logs,response);
    }

    /**
     * 删除所有ERROR日志
     * @return
     */
    @com.lcu.wl.annotation.Log("删除所有ERROR日志")
    @ApiOperation("清空所有异常日志")
    @DeleteMapping("/error/delete")
    @RequiresPermissions("mnt:errLog:delete")
    public SYSDATA deleteErr(){
        iLogService.removeByMap(new MapUtils().put("log_type","ERROR"));
        return SYSDATA.ok();
    }
}
